/**
 * 
 */
package cn.hbu.zlfw.exchange.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.hbu.zlfw.admin.entity.Expert;
import cn.hbu.zlfw.admin.entity.RootAdmin;
import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.admin.entity.UnitAdmin;
import cn.hbu.zlfw.admin.service.ExpertService;
import cn.hbu.zlfw.admin.service.RootAdminService;
import cn.hbu.zlfw.admin.service.ScienceExpertService;
import cn.hbu.zlfw.common.base.dao.impl.BaseDaoImpl;
import cn.hbu.zlfw.common.util.DicUtil;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.dic.dao.DicUserTypeDao;
import cn.hbu.zlfw.dic.entity.DicUserType;
import cn.hbu.zlfw.exchange.dao.ChatDao;
import cn.hbu.zlfw.exchange.dao.PosterDao;
import cn.hbu.zlfw.exchange.entity.Chat;
import cn.hbu.zlfw.exchange.entity.Poster;
import cn.hbu.zlfw.research.entity.Researcher;
import cn.hbu.zlfw.research.service.ResearcherService;

/**
 * @ClassName: ChatDaoImpl
 * @Description: 在线交流dao实现类
 * @author tiddar
 * @date 2018年8月21日
 *
 */
@Repository("chatDao")
public class ChatDaoImpl extends BaseDaoImpl<Chat, String> implements ChatDao {

	
	@Autowired
	private ScienceExpertService scienceExpertService;
	@Autowired
	private ResearcherService researcherService;
	@Autowired
	ExpertService expertService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.hbu.zlfw.exchange.dao.ChatDao#listPostToPatent(cn.hbu.zlfw.common.util.
	 * PageModel, java.lang.String)
	 */
	@Override
	public PageModel<Chat> listChatToPatent(PageModel<Chat> pageModel, String patentId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", patentId);
		StringBuffer sbfhql = new StringBuffer();
		StringBuffer sbfhqlCount = new StringBuffer();
		sbfhql.append("from Chat ch inner join fetch ch.patent pt where pt.id=:id ");
		sbfhqlCount.append("select count(*) from Chat ch inner join ch.patent pt where pt.id=:id ");
		sbfhql.append(" order by ch.updateTime asc");
		PageModel<Chat> chatpage = this.findListByParams(sbfhql.toString(), sbfhqlCount.toString(), params, pageModel);
		List<Chat> chats = chatpage.getList();
		chats.stream().forEach(chat -> {
			Integer userType = Integer.valueOf(chat.getFromUserType());
			if (DicUserType.SCIENCE_SERVICE_USER.equals(userType)) { // 科技服务人员
				ScienceExpert scienceExpert = this.scienceExpertService.getById(chat.getFromUserId());
				chat.setUser(scienceExpert);
			} else if (DicUserType.RESEARCHER_USER.equals(userType)) { // 科研人员
				Researcher researcher = this.researcherService.getById(chat.getFromUserId());
				chat.setUser(researcher);
			} else if (DicUserType.EXPERT_SERVICE_USER.equals(userType)) { // 专家
				Expert expert = this.expertService.getById(chat.getFromUserId());
				chat.setUser(expert);
			}
		});
		chatpage.setList(chats);
		return chatpage;
	}
}
